package com.xiaowei.system.controller;

import com.xiaowei.response.Result;
import com.xiaowei.system.service.AliOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author CWei
 * @Date 2021/10/7 21:31
 * @Version 1.0
 */
@Api
@RestController
@CrossOrigin
public class AliOssController {
    @Autowired
    private AliOssService aliOssService;

    @ApiOperation(value = "上传图片文件")
    @PostMapping("/uploadImgFile")
    public Result uploadImgFile(MultipartFile file){
        String s = aliOssService.upload(file);
        return Result.ok().data("url",s);
    }

    @ApiOperation(value = "删除替换之后的头像")
    @PostMapping("/deleteImgFile")
    public Result deleteImgFile(String file){
        //https://xinguanwuzi-parent.oss-cn-beijing.aliyuncs.com/2021/10/07/1b81281429a9439495a91533e98fc5a7.jpg
       try{
           String[] split = file.split(".com/");
           System.out.println(split[1]);
           aliOssService.deleteFile(split[1]);
           return Result.ok();
       }catch (Exception e){
           //需要打印错误日志到本地系统中（服务器系统）
           return  Result.error();
       }
    }
}
